import { Component, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { myValidNull } from '@core/util/string-utils';
import { NzMessageService } from 'ng-zorro-antd';
import { SalesmanService } from '@core/routes-services/salesman.service';
import { MarketSalesmanEditComponent } from '../edit/edit.component';
import { DistrictService } from '@core/routes-services/district.service';
import { SetPasswordComponent } from '../../set-password/set-password.component';
import { ActivatedRoute } from '@angular/router';
import { SalesmanTypeService } from '@core/routes-services/salesman-type.service';
import { SalesmanRoleSettingService } from '@core/routes-services/salesman-role-setting.service';

@Component({
  selector: 'app-market-salesman-list',
  templateUrl: './list.component.html',
})
export class MarketSalesmanListComponent implements OnInit {
  isLoading = false;
  keyword: any;
  districtId:any;
  role:any = 1;//角色: 1-业务员，2-渠道商，3-门店，4-代理
  type:any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  districtOption:any [] = [];
  salesManTypeOption:any [] = [];

  roleSetting:any = {};
  constructor(private message: NzMessageService,
              private route:ActivatedRoute,
              private salesmanService:SalesmanService,
              private districtService:DistrictService,
              private salesmanTypeService:SalesmanTypeService,
              private salesmanRoleSettingService:SalesmanRoleSettingService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initDistrice();
    this.route.params.subscribe((params)=>{
      this.role = +params['role'];
      this.initPage();
    })
  }

  initDistrice(){
    this.districtService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.districtOption = res.data;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
    this.salesmanTypeService.getAll().then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.salesManTypeOption = res.data.marketSalesmanTypeList;
        if(this.role == 3){
          this.salesManTypeOption = this.salesManTypeOption.filter((d)=>d.type == 1);
        }else {
          this.salesManTypeOption = this.salesManTypeOption.filter((d)=>d.type == 2);
        }
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }

  initPage() {
    this.isLoading = true;
    this.salesmanService.pagination(this.page, this.keyword,this.districtId,this.role,this.type).then((res) => {
      if (res.status.toString().startsWith("20")) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error)
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(MarketSalesmanEditComponent, { entity: bak,districtOption:this.districtOption,salesManTypeOption:this.salesManTypeOption }, 600, {
      nzTitle:temp ? '添加星创股东' : '【'+entity.name+'】的详细信息',
    }).subscribe(result => {
      if(result){
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  setPassword = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(SetPasswordComponent, { id: entity.id,businessService:this.salesmanService }, 560, {
      nzTitle: '为【'+entity.name+'】重置密码',
    }).subscribe(result => {
      if(result){
        this.message.success("修改成功");
        // this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  delete = (data) => {
    // this.bannerService.deleteObj(data.id).then((data) => {
    //   this.search();
    //   this.message.success('删除成功');
    // }, error => console.log('删除出错，后续处理'));
  };


}
